package zhoufu.sz.sanxiao_check.presents;

import javax.inject.Inject;
import zhoufu.sz.sanxiao_check.models.TrainListModel;
import zhoufu.sz.sanxiao_check.net.BasicRequestRepository;
import zhoufu.sz.sanxiao_check.net.RequestHelper;
import zhoufu.sz.sanxiao_check.ui.contract.StreetListContract;
import zhoufu.sz.sanxiao_check.ui.contract.TrainListContract;

/**
 * User:RCP
 * email:ruancaipu@gmail.com
 * Date:2016/7/21
 */
public class TrainListPresenter implements StreetListContract.Presenter
{
    private BasicRequestRepository repository;

    private TrainListContract.View view;

    private int pageIndex = 1;

    private int totalSize;


    @Inject
    public TrainListPresenter(BasicRequestRepository repository)
    {
        this.repository = repository;
    }

    public void bindView(TrainListContract.View view)
    {
        this.view = view;
    }

    @Override public void onStart(Class c)
    {

    }

    @Override public void next()
    {
        pageIndex++;
        onStart();
    }


    @Override public void onStart()
    {
        repository.GetStreetTrainByPage(view.SID(), pageIndex,
                new RequestHelper.RequestCallBack<TrainListModel>()
                {
                    @Override public void onPullStart()
                    {
                        if ( pageIndex == 1 )
                        {
                            view.vStart();
                        }
                    }

                    @Override public void onPullFailud(Throwable throwable)
                    {
                        if ( pageIndex == 1 )
                        {
                            view.pullFaild();
                        }
                    }

                    @Override public void onPullEnd()
                    {

                    }

                    @Override public void onPullSuccessed(TrainListModel s)
                    {
                        view.completedLoadMore();
                        totalSize = s.getPageCount();
                        if ( s.getErrNum().equals("0") )
                        {
                            if ( s.getData().size() == 0 && pageIndex == 1 )
                            {
                                view.showEmptyView();
                            } else
                            {
                                view.setAdapterData(s.getData(), pageIndex);
                                view.pullEnd();
                            }
                        } else
                        {
                            view.pullFaild();
                            view.showMessage(s.getErrMsg());
                        }
                    }
                });
    }

    @Override public void onPause()
    {

    }

    @Override public void onDestroy()
    {
        repository.onDestory();
    }

    public void setPageFirst()
    {
        pageIndex = 1;
    }


    public int getTotalSize()
    {
        return totalSize;
    }
}
